//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
#pragma link "CComboBx"
#pragma link "CEdit"
#pragma resource "*.dfm"

//---------------------------------------------------------------------------
#include "vSDesignationManquante.h"
//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TRefMultiple::TRefMultiple(const int &nNbRef,
                                      const int &nLogiciel,
                                      const int &nVersion,
                                      const AnsiString &asRefNom,
                                      const AnsiString &asRefDesignation,
                                      const AnsiString &asRefAdresse,
                                      const int &nCbAttente,
                                      TComponent* Owner)
    : TForm(Owner)
{

    TZZMySqlQuery * MaRequete = NULL;
    CComboAttente * Attente = NULL;
    try {
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        //Remplissage de la combo attente
        //-------------------------------
        Attente = new CComboAttente();
        Attente->Remplir(cbAttente->Items);

        //bConsulter->Glyph->LoadFromResourceName((int)HInstance, "ID_ACTION_CONSULTER");

        lEntete->Caption = PAGE_MULTIPLE;

        //Initialiser les variables
        //-------------------------

        m_nIndexPanel = 0;
        m_nLogiciel = nLogiciel;
        m_nVersion  = nVersion;
        m_asRefNom  = asRefNom;


        AfficherRefMultiple(nNbRef);

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select log_nom,"+       //0
                                                    "ver_nom,"+      //1
                                                    "ref_sous_nom,"+ //2
                                                    "ref_adresse,"+  //3
                                                    "ref_attente "+  //4
                                            "from refer "+
                                                "left outer join logiciels on "+
                                                    "(refer.log_code = logiciels.log_code) "+
                                                "inner join versions on "+
                                                    "(refer.ver_code = versions.ver_code) "+
                                            "where (refer.log_code = "+m_nLogiciel+" and refer.ver_code = "+m_nVersion+") and ref_nom = "+QuotedStr(asRefNom);
        MaRequete->Open();
        MaRequete->First();

        eLogiciel->Text    =   asRequete(0);
        eVersion->Text     =   asRequete(1);
        eReference->Text   =   asRefNom;

        if( !asRefDesignation.IsEmpty() )
            eDesignation->Text = asRefDesignation;

        if( !asRefAdresse.IsEmpty())
            ePageWeb->Text = asRefAdresse;

        while(!MaRequete->Eof) {
            RenseignerChamp(asRequete(2), asRequete(3), nRequete(4));
            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();

        VerifierDesignation();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(Attente) {delete Attente; Attente = NULL;}
}
//---------------------------------------------------------------------------
__fastcall TRefMultiple::~TRefMultiple()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              AFFICHER REFERENCE MULTIPLE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherRefMultiple( const int &nNbreRef )
{
    try {
        TPanel * Panel = NULL;
        for(int i=0; i<(nNbreRef); i++) {
            Panel               = new TPanel(pReste);
            Panel->Name         = (AnsiString)"Panel"+i;
            AfficherZonePanel( Panel );
        }

        //Calcul du Range pour la ScrollBox
        //---------------------------------
        sbAdresseMultiple->VertScrollBar->Range = 25 + (80*(nNbreRef));
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              STRUCTURE ZONE PANEL
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherZonePanel( TPanel * Panel )
{
    try {
        CEdit       * Designation;
        CEdit       * PageWeb;
        TLabel      * lDesignation;
        TLabel      * lPageWeb;
        TLabel      * lAttente;
        CComboBox   * cbAttente;
        TBitBtn     * bConsulter;

        Panel->Parent       = pReste;
        Panel->Align        = alTop;
        Panel->Width        = 502;
        Panel->Height       = 80;
        Panel->Color        = clBtnFace;

        cbAttente           = new CComboBox(Panel);     //Composant 0
        PageWeb             = new CEdit(Panel);         //Composant 1
        Designation         = new CEdit(Panel);         //Composant 2
        lDesignation        = new TLabel(Panel);        //Composant 3
        lPageWeb            = new TLabel(Panel);        //Composant 4
        lAttente            = new TLabel(Panel);        //Composant 5
        bConsulter          = new TBitBtn(Panel);       //Composant 6

        //Designation
        //-----------
        Designation->Parent = Panel;
        Designation->Left   = 112;
        Designation->Top    = 4;
        Designation->Width  = 382;
        Designation->Height = 21;
        Designation->Obligatoire = true;
        Designation->Name   = "Designation";
        Designation->Text   = "";

        lDesignation->Parent= Panel;
        lDesignation->Caption = "Désignation";
        lDesignation->Font->Style = TFontStyles()<< fsBold << fsUnderline;
        lDesignation->Left  = 8;
        lDesignation->Top   = 4;

        //Page Web
        //--------
        PageWeb->Parent     = Panel;
        PageWeb->Name       = "PageWeb";
        PageWeb->Left       = 112;
        PageWeb->Top        = 28;
        PageWeb->Width      = 382;
        PageWeb->Height     = 21;
        PageWeb->Color      = clWindow;
        PageWeb->Text       = "";
        PageWeb->ReadOnly   = true;

        lPageWeb->Parent    = Panel;
        lPageWeb->Caption   = "Page Web";
        lPageWeb->Left      = 8;
        lPageWeb->Top       = 28;

        bConsulter->Parent  = Panel;
        bConsulter->Name    = "Consulter";
        bConsulter->Caption = "";
        bConsulter->Left    = 72;
        bConsulter->Top     = 21;
        bConsulter->Height  = 20;
        bConsulter->Width   = 25;
        bConsulter->NumGlyphs = 2;
        AnsiString toto = IncludeTrailingBackslash(ExtractFilePath(ParamStr(0)));
        bConsulter->Glyph->LoadFromFile("M:\\Utilitaires EIC\\WebHelpIcones\\globe.bmp");

        //Page d'attente
        //--------------
        cbAttente->Parent     = Panel;
        cbAttente->Name       = "Attente";
        cbAttente->Text       = "";  
        cbAttente->Left       = 112;
        cbAttente->Top        = 52;
        cbAttente->Width      = 382;
        cbAttente->Height     = 21;
        cbAttente->Style      = csDropDownList;

        //Remplissage de la combo attente
        //-------------------------------
        CComboAttente * Attente = NULL;
        Attente = new CComboAttente();
        Attente->Remplir(cbAttente->Items);
        if(Attente) {delete Attente; Attente = NULL;}

        lAttente->Parent     = Panel;
        lAttente->Caption    = "Page d'attente";
        lAttente->Left       = 8;
        lAttente->Top        = 52;

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//              UTILISATION DES COMPOSANTS DYNAMIQUE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::RenseignerChamp(  const AnsiString &asDesignation,
                                                const AnsiString &asPageWeb,
                                                const int &nCbAttente)
{
    try {
        //Permet de nous informer sur le nom du panel utilisé
        //---------------------------------------------------
        TComponent * pChildPanel = pReste->Components[m_nIndexPanel];

        //Composant fils du panel
        //-----------------------
        for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {

            TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];

            if( dynamic_cast<CEdit*>(pChildFilsPanel)) {
                if( ((CEdit*)pChildFilsPanel)->Name == "Designation") {
                    ((CEdit*)pChildFilsPanel)->Text = asDesignation;
                }

                if( ((CEdit*)pChildFilsPanel)->Name == "PageWeb") {
                    ((CEdit*)pChildFilsPanel)->Text = asPageWeb;
                }
            }

            if( dynamic_cast<TButton*>(pChildFilsPanel)) {
                if( ((TButton*)pChildFilsPanel)->Name == "Consulter") {
                    ((TButton*)pChildFilsPanel)->OnClick = AfficherPageWeb;
                }
            }

            if( dynamic_cast<CComboBox*>(pChildFilsPanel)) {
                if( ((CComboBox*)pChildFilsPanel)->Name == "Attente") {
                    ((CComboBox*)pChildFilsPanel)->ItemIndex = nCbAttente;
                    ((CComboBox*)pChildFilsPanel)->OnChange  = OnChangeComboAttente;
                }
            }
        }
        //Prochain panel
        //--------------
        m_nIndexPanel++;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          Evenement OnChange sur les combobox page d'attente dynamique
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::OnChangeComboAttente(TObject *Sender)
{
    try {
        //Recuperation de la combo utilisé
        //--------------------------------
        CComboBox * ComboAttente = dynamic_cast<CComboBox*>(Sender);

        //Parent de la combo
        //------------------
        TPanel * MonPanel = dynamic_cast<TPanel*>(ComboAttente->Parent);

        for(int i=0; i<MonPanel->ComponentCount;i++) {
            TComponent * pFils  = MonPanel->Components[i];
            if( dynamic_cast<CEdit*>(pFils)) {
                if( ((CEdit*)pFils)->Name == "Designation") {
                    ((CEdit*)pFils)->Obligatoire = false;
                }
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//      Evenement OnChange sur la combobox page d'attente statique
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::cbAttenteChange(TObject *Sender)
{
    try {
        eDesignation->Obligatoire = false;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//                  Rafraichir les combos attente
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::ImgRefreshClick(TObject *Sender)
{
    try {
        //combo statique
        cbAttente->ItemIndex = AUCUNE_SELECTION;
        eDesignation->Obligatoire = true;

        //Combo dynamique
        for(int i=0; i<pReste->ComponentCount; i++) {
            TComponent * pChildPanel = pReste->Components[i];
            for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {
                TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];

                //Composant CComboBox
                //-------------------
                if( dynamic_cast<CComboBox*>(pChildFilsPanel)) {
                    if( ((CComboBox*)pChildFilsPanel)->Name == "Attente") {
                        ((CComboBox*)pChildFilsPanel)->ItemIndex = AUCUNE_SELECTION;
                    }
                }

                //Composant CEdit
                //---------------
                if( dynamic_cast<CEdit*>(pChildFilsPanel)) {
                    if( ((CEdit*)pChildFilsPanel)->Name == "Designation") {
                        ((CEdit*)pChildFilsPanel)->Obligatoire = true;
                    }
                }
            }
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
//          AFFICHER PAGE WEB
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::bConsulterClick(TObject *Sender)
{
    CVerifInternet * VerifInternet = NULL;
    try {
        VerifInternet = new CVerifInternet();
        if(!Trim(ePageWeb->Text).IsEmpty()) {
            if(VerifInternet->ConnexionAuSite(ePageWeb->Text))
                ShellExecute(NULL, "Open", ePageWeb->Text.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
        }else
            MessageDlg("Veuillez remplir le champ Page Web", mtWarning, TMsgDlgButtons() << mbOK, 0);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(VerifInternet) {delete VerifInternet; VerifInternet = NULL;}

}
//---------------------------------------------------------------------------
//          AFFICHER PAGE WEB VENANT DES BOUTONS DYNAMIQUE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherPageWeb(TObject *Sender)
{
    CVerifInternet * VerifInternet = NULL;
    try {
        AnsiString asAdresse = "";
        //Recuperation du bouton appuyé
        //-----------------------------
        TButton * MonBouton = dynamic_cast<TButton*>(Sender);
        //Parent du bouton
        //----------------
        TPanel * MonPanel = dynamic_cast<TPanel*>(MonBouton->Parent);

        for(int i=0; i<MonPanel->ComponentCount;i++) {
            TComponent * pFils  = MonPanel->Components[i];
            if( dynamic_cast<CEdit*>(pFils)) {
                if( ((CEdit*)pFils)->Name == "PageWeb") {
                    asAdresse = ((CEdit*)pFils)->Text;
                }
            }
        }

        VerifInternet = new CVerifInternet();
        if(!Trim(asAdresse).IsEmpty()) {
            if(VerifInternet->ConnexionAuSite(ePageWeb->Text))
                ShellExecute(NULL, "Open", asAdresse.c_str(),NULL, NULL, SW_SHOWMAXIMIZED);
        }else
            MessageDlg("Veuillez remplir le champ Page Web", mtWarning, TMsgDlgButtons() << mbOK, 0);

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(VerifInternet) {delete VerifInternet; VerifInternet = NULL;}

}
//---------------------------------------------------------------------------
//                          VALIDATION
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::bbOkClick(TObject *Sender)
{
    TStringList * ListDesignation = NULL;
    TStringList * ListPageWeb     = NULL;
    TStringList * lAttente        = NULL;
    TStringList * lRefCode        = NULL;
    try {
        if( VerifierDesignation() ) {

            ListDesignation = new TStringList();
            ListPageWeb     = new TStringList();
            lAttente        = new TStringList();

            for(int i=0; i<pReste->ComponentCount; i++) {
                TComponent * pChildPanel = pReste->Components[i];
                for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {
                    TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];
                    //Composant CComboBox
                    //-------------------
                    if( dynamic_cast<CComboBox*>(pChildFilsPanel)) {
                        if( ((CComboBox*)pChildFilsPanel)->Name == "Attente") {
                            lAttente->Add(((CComboBox*)pChildFilsPanel)->ItemIndex);
                        }
                    }

                    //Composant CEdit
                    //---------------
                    if( dynamic_cast<CEdit*>(pChildFilsPanel)) {

                        if( ((CEdit*)pChildFilsPanel)->Name == "Designation") {
                            ListDesignation->Add(((CEdit*)pChildFilsPanel)->Text);
                        }

                        if( ((CEdit*)pChildFilsPanel)->Name == "PageWeb") {
                            ListPageWeb->Add(((CEdit*)pChildFilsPanel)->Text);
                        }
                    }
                }
            }
            lRefCode = new TStringList();
            for( int i=0; i<ListPageWeb->Count; i++)
                lRefCode->Add(GetRefCode(ListPageWeb->Strings[i]));

            //Insertion dans la base WebHelp
            //------------------------------
            dmWebHelp->tRefer->Active = true;
            dmWebHelp->tRefer->Insert();

            ntRefer(LOG_CODE)           = m_nLogiciel;
            ntRefer(VER_CODE)           = m_nVersion;
            astRefer(REF_NOM)           = m_asRefNom;
            if(!eDesignation->Text.IsEmpty())
                astRefer(REF_SOUS_NOM)  = eDesignation->Text;
            astRefer(REF_ADRESSE)       = ePageWeb->Text;
            ntRefer(REF_ATTENTE)        = cbAttente->ItemIndex;

            dmWebHelp->tRefer->Post();
            dmWebHelp->trWebHelp->Commit();

            //Mise à jour des champ désignation
            //---------------------------------
            int nIdxDesignation = 0;
            dmWebHelp->tRefer->Active = true;
            for(int i=0; i<lRefCode->Count; i++) {
                dmWebHelp->tRefer->First();
                while(!dmWebHelp->tRefer->Eof) {
                    if(ntRefer(REF_CODE) == lRefCode->Strings[i].ToInt()) {
                        dmWebHelp->tRefer->Edit();
                        if(lAttente->Strings[i].ToInt() == AUCUNE_SELECTION)
                            astRefer(REF_SOUS_NOM)  = ListDesignation->Strings[nIdxDesignation];
                        ntRefer(REF_ATTENTE) = lAttente->Strings[i].ToInt();
                        dmWebHelp->tRefer->Post();
                        dmWebHelp->trWebHelp->Commit();
                        break;
                    }
                    dmWebHelp->tRefer->Next();
                }
                nIdxDesignation++;
            }
            //Validation OK
            //-------------
            ModalResult = mrOk;
        }else
            ModalResult = mrNone;

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if( lRefCode )        {delete lRefCode; lRefCode = NULL;}
    if( lAttente )        {delete lAttente; lAttente = NULL;}
    if( ListPageWeb )     {delete ListPageWeb; ListPageWeb = NULL;}
    if( ListDesignation ) {delete ListDesignation; ListDesignation = NULL;}
}
//---------------------------------------------------------------------------
//                  RECUPERATION DU REF_CODE
//---------------------------------------------------------------------------
int  __fastcall TRefMultiple::GetRefCode( const AnsiString &asPageWeb )
{
    int RefCode = 0;
    TZZMySqlQuery * MaRequete = NULL;
    try {
        //Recuperer le ref_code permettant d'inserer au bon endroit la désignation
        //------------------------------------------------------------------------
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ref_code "+
                                            "from refer where (log_code="+m_nLogiciel+" and ver_code="+m_nVersion+") "+
                                            "and (ref_nom="+QuotedStr(m_asRefNom)+" and ref_adresse="+QuotedStr(asPageWeb)+")";
        MaRequete->Open();
        MaRequete->First();

        RefCode = nRequete(0);

        MaRequete->Sql->Clear();
        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        RefCode = 0;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    return RefCode;
}
//---------------------------------------------------------------------------
//                  VERIFIER LES DESIGNATIONS NON REMPLIES
//---------------------------------------------------------------------------
bool __fastcall TRefMultiple::VerifierDesignation()
{
    bool bRetour = false;
    TStringList * lVerif = NULL;
    try {
        lVerif = new TStringList();

        bool bAttente = false;

        if(!Trim(eDesignation->Text).IsEmpty()) {
            lVerif->Add(1);
        }else {
            if(cbAttente->ItemIndex != AUCUNE_SELECTION){
                lVerif->Add(1);
                eDesignation->Obligatoire = false;
            }else {
                lVerif->Add(0);
            }
        }

        for(int i=0; i<pReste->ComponentCount; i++) {
            TComponent * pChildPanel = pReste->Components[i];
            for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {
                TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];

                //Composant CComboBox
                //-------------------
                if( dynamic_cast<CComboBox*>(pChildFilsPanel)) {
                    if( ((CComboBox*)pChildFilsPanel)->Name == "Attente") {
                        if(((CComboBox*)pChildFilsPanel)->ItemIndex != AUCUNE_SELECTION){
                            lVerif->Add(1);
                            bAttente = true;
                        }
                    }
                }

                //Composant CEdit
                //---------------
                if( dynamic_cast<CEdit*>(pChildFilsPanel)) {
                    if( ((CEdit*)pChildFilsPanel)->Name == "Designation") {
                        if( !Trim(((CEdit*)pChildFilsPanel)->Text).IsEmpty() )
                            lVerif->Add(1);
                        else {
                            if( bAttente ){
                                ((CEdit*)pChildFilsPanel)->Obligatoire = false;
                                lVerif->Add(1);
                            }else
                                lVerif->Add(0);
                        }
                    }
                }
            }
        }

        for(int i=0; i<lVerif->Count; i++) {
            if(lVerif->Strings[i].ToInt() == 0) {
                bRetour = false;
                break;
            }else
                bRetour = true;
        }

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;}
    if(lVerif) {delete lVerif; lVerif = NULL;}
    return bRetour;
}
//---------------------------------------------------------------------------
